package xiaobange.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xiaobange.common.CustomException;
import xiaobange.entity.Category;
import xiaobange.entity.Dish;
import xiaobange.entity.Setmeal;
import xiaobange.mapper.CategoryMapper;
import xiaobange.service.CategoryService;
import xiaobange.service.DishService;
import xiaobange.service.SetmealService;

@Service
@Slf4j
public class CategoryServiceImp extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void DeleteCheck(Long id) {
        //查询当前分类是否关联了菜品，如果关联了则抛出一个异常
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(dishLambdaQueryWrapper);
        if (count1 > 0){
            //抛出异常
            throw new CustomException("当前菜品分类下关联了相关菜品，不能删除！");
        }
        //查询当前分类是否关联了套餐，如果关联了则抛出一个异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);
        if (count2 > 0){
            //抛出异常
            throw new CustomException("当前套餐分类下关联了相关菜品，不能删除！");
        }
        //正常执行
        super.removeById(id);
    }
}
